home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15742 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  44 lines

  1. Newsgroups: comp.lang.c++
  2. Path: cdf.toronto.edu!news
  3. From: p a u l <a209dowm@cdf.utoronto.ca>
  4. Subject: Re: file descriptors and fstreams...
  5. Content-Type: text/plain; charset=us-ascii
  6. Message-ID: <DpI5sI.EGx@cdf.toronto.edu>
  7. Sender: news@cdf.toronto.edu (Usenet News)
  8. Nntp-Posting-Host: sat
  9. Content-Transfer-Encoding: 7bit
  10. Organization: Computing Disciplines Facility, University of Toronto
  11. References: <4k780i$dd5@beyond.escape.com>
  12. Mime-Version: 1.0
  13. Date: Sun, 7 Apr 1996 17:26:39 GMT
  14. X-Mailer: Mozilla 1.1N (X11; I; SunOS 5.3 sun4c)
  15. X-Url: news:4k780i$dd5@beyond.escape.com
  16.  
  17. >Hi I'm a very beginner c++ programmer, although I've been doing C for a
  18. >while. I know there's a C function called fdopen which takes a file
  19. >descriptor and creates a stream for it so you can use fprintf, fscanf,
  20. >and all that other good stuff. Is there a similar function (or whatever)
  21. >that takes a file descriptor and does something to let you use << and >>
  22. >with it?
  23.  
  24. I think the equivalent to fdopen() is this constructor:
  25.  
  26. ifstream::ifstream (int fd)  // fd is an already open file descriptor
  27.  
  28. so you would use:
  29. ifstream in_file(5);   // using file descriptor 5
  30.  
  31. to create a new ifstream object that reads from an already open file
  32. descriptor.
  33. see this url for more info on iostreams with g++:
  34. http://www.cygnus.com/doc/iostream_24.html#SEC24
  35.  
  36. I haven't been able to get it to work myself though, perhaps it's because I was
  37. trying to use it on a file descriptor that is actually a pipe.  can anyone tell
  38. m if that should work?  my (unsatisfactory) workaround is to write to a string
  39. with an strstream object and then use write() to send that to the file
  40. descriptor.  is there a better way?
  41.  
  42. Paul.
  43.  
  44.